//
//  GYSortView.m
//  GoodLuck
//
//  Created by apple on 2018/3/7.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//

#import "GYSortView.h"
#import "GYSortCell.h"

@interface GYSortView ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

/** 表格数据源 */
@property (nonatomic, strong) NSArray *sortTitles;

/** 默认选中哪个 */
@property (nonatomic, assign) NSInteger index;
@end

@implementation GYSortView

- (void)awakeFromNib {
    [super awakeFromNib];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.index = 0;
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.sortTitles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GYSortCell *cell = [GYSortCell cellWithTableView:tableView];
    cell.sortLable.text = self.sortTitles[indexPath.row];
    if (self.index == indexPath.row) {
        cell.selectState = YES;
    }else {
        cell.selectState = NO;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.index = indexPath.row;
    [tableView reloadData];
    
    self.sortResultCallBack(indexPath.row,self.sortTitles[indexPath.row]);
}

#pragma mark -- 懒加载

- (NSArray *)sortTitles {
    if (!_sortTitles) {
        _sortTitles = @[@"综合排序",@"价格从高到低",@"价格从低到高",@"人气排序"];
    }
    
    return _sortTitles;
}
@end
